local DisplayBar = {} 

DisplayBar.__index = DisplayBar

setmetatable(DisplayBar, {
	__call = function (cls, ...)
		return cls.new(...)
	end,
})

function DisplayBar.new(xmlDoc, block, msg)
	local self = setmetatable({}, DisplayBar)
	self.text = Raiderino.L[msg.text]
	self.MaxTime = msg.maxtime
	self.EndTime = GameLib.GetGameTime() + msg.maxtime
	self.destroyAtEnd = msg.destroyAtEnd
	self.notified = false

	self.Frame = Apollo.LoadForm(xmlDoc, "BarTemplate", block.ContentList, self)
	self.Frame:FindChild("Text"):SetText(self.text)
	if msg.icon then
		self.Frame:FindChild("Icon"):SetSprite(msg.icon)
	end
	self.Frame:FindChild("RemainingOverlay"):SetMax(msg.maxtime)
	self.Frame:SetSprite("BarTextures_Fire")
	self.Frame:FindChild("RemainingOverlay"):SetFullSprite("BarTextures_Fire")
	return self
end

--------------------
-- BAR MANAGEMENT --
--------------------
function DisplayBar:SetTimer(timeRemaining)
	if timeRemaining > 0 then
		self.Frame:FindChild("RemainingOverlay"):SetProgress(self.MaxTime - timeRemaining)
	else
		self.Frame:FindChild("RemainingOverlay"):SetProgress(self.MaxTime)
	end
	
	self.Frame:FindChild("Timer"):SetText(string.format("%.1fs", timeRemaining))
	local frameData = self.Frame:GetData()
	frameData.remains = timeRemaining
	self.Frame:SetData(frameData)
end
---------------------
-- /BAR MANAGEMENT --
---------------------

--------------------
-- GUI MANAGEMENT --
--------------------
function DisplayBar:SetHeight(height)
	local left, top, right, bottom = self.Frame:GetAnchorOffsets()
	self.Frame:SetAnchorOffsets(left, top, right, top + height)

	local icon = self.Frame:FindChild("Icon")
	local iconHeight = icon:GetHeight()
	local left, top, right, bottom = icon:GetAnchorOffsets()
	icon:SetAnchorOffsets(left, top, left + iconHeight, bottom)

	local text = self.Frame:FindChild("Text")
	local left, top, right, bottom = text:GetAnchorOffsets()
	text:SetAnchorOffsets(iconHeight + 9, top, right, bottom)
end

function DisplayBar:SetBGColor(color)
	self.Frame:SetBGColor(color)
end

function DisplayBar:SetBarColor(color)
	test = self.Frame:FindChild("RemainingOverlay")
	self.Frame:FindChild("RemainingOverlay"):SetBarColor(color)
end
---------------------
-- /GUI MANAGEMENT --
---------------------

if _G["RaiderinoLibs"] == nil then
	_G["RaiderinoLibs"] = { }
end
_G["RaiderinoLibs"]["DisplayBar"] = DisplayBar